package com.yanqu.road.server.cmd.pageant;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.game.result.UserPageantDataResult;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.pb.PageantPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = Protocol.S_PAGEANT_USE_TOOL, desc = "道具使用")
public class PageantUseGoodsCmd extends AbstractCommand {

    private static Logger logger = LogManager.getLogger(PageantUseGoodsCmd.class.getName());

    private int activityId;

    private int propId;

    private int pramsFirst;

    private UserPageantDataResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.PageantToolUseReqMsg reqMsg = PageantProto.PageantToolUseReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        pramsFirst = reqMsg.getPramsFirst();
        propId = reqMsg.getPropId();
        if (pramsFirst < 0) {
            return false;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        if (activityInfo == null || !ActivityMgr.activityInTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (propId == 0) {
            ret = GameErrorCode.E_PAGEANT_MOVEDIRECTION_EMPTY;
            return;
        }

        result = player.getModule(PageantModule.class).useGood(activityInfo.getActivityId(), propId, pramsFirst);
    }


    @Override
    public void sendData(GamePlayer player) {
        PageantProto.PageantToolUseRespMsg.Builder respMsg = PageantProto.PageantToolUseRespMsg.newBuilder();
        if(0==ret) {
            respMsg.setRet(result.getRet());
            if (0 == result.getRet()) {
                if(null!=result.getUserPageantData()) {
                    respMsg.setEntity(PageantPb.parsePageantBaseRespMsg(result.getUserPageantData()));
                }
            }
        }else {
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_PAGEANT_USE_TOOL, respMsg);
    }
}
